using System;
using System.Collections.Generic;
using System.Text;

using System.Xml;
using System.Reflection;

using OWED.Network.Messages;
using OWED.Network.Messages.Reply;

namespace OWED.Network
{
    public class MessageGet
    {

		/// <summary>
		/// This returns a valid ProtocolItem if your doc is valid within the
		/// set of protocol messages.  If it is not, it returns null.
		/// 
		/// This method is constant.
		/// </summary>
		/// <param name="doc">The message in xml form</param>
		/// <returns>A protocol item or null.</returns>
        public static IProtocolItem GetFromXml(XmlDocument doc)
        {
			Type t = Type.GetType("OWED.Network.Messages." + 
				doc.DocumentElement.Name);
			if (t == null) {
				t = Type.GetType("OWED.Network.Messages.Reply." +
				doc.DocumentElement.Name);
			}
			if (t == null) {
				return null;
			}
			try {
				MethodInfo from_method = t.GetMethod("FromXml");
				object[] arguments = new object[1];
				arguments[0] = doc;
				return from_method.Invoke(null, arguments) as IProtocolItem;
			} catch (Exception) { //anything within here is caused by
									//an issue with the document.
				return null;
			}
        }
    }
}
